-
Notifications
You must be signed in to change notification settings - Fork 907
Implement ES2024 Resizable ArrayBuffer #2182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Adds support for resizable ArrayBuffers as specified in ES2024.
Constructor accepts optional maxByteLength parameter:
new ArrayBuffer(length, { maxByteLength })
New resize() method allows in-place buffer resizing:
- Grows or shrinks buffer up to maxByteLength
- Preserves existing data, zeros new bytes
- Throws TypeError if not resizable or detached
- Throws RangeError if exceeds maxByteLength
New properties:
- resizable: true if buffer has maxByteLength
- maxByteLength: maximum size (equals byteLength for fixed-length)
Includes 22 comprehensive tests covering all functionality and edge cases.
All tests passing.
0bbd0a0 to
088d376
Compare
Removed resizable-arraybuffer from unsupported features list.
Updated test262.properties removing {unsupported: [resizable-arraybuffer]} markers from all passing tests.
gbrail
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this and for your patience as we ask lots of questions...
rhino/src/main/java/org/mozilla/javascript/typedarrays/NativeArrayBuffer.java
Outdated
Show resolved
Hide resolved
Address review feedback by using primitive int instead of boxed Integer for maxByteLength field. Uses -1 as sentinel value for fixed-length buffers instead of null. Benefits: - Better performance (no boxing/unboxing overhead) - Less memory usage (no object wrapper) - Follows Rhino coding conventions - More cache-friendly Changes: - NativeArrayBuffer.java: Changed field from Integer to int - Updated all null checks to use -1 sentinel value - All tests passing (Test262SuiteTest + ResizableArrayBuffer tests)
Add support for TypedArray views that dynamically track the length of resizable ArrayBuffers. When a TypedArray is constructed without an explicit length parameter, it now operates in auto-length mode where the length is recomputed based on the current buffer size. Implementation: - Added boolean isAutoLength field to track auto-length mode - Removed final modifier from length field to allow dynamic updates - Created updateLength() method that recomputes length for auto-length views - Length is computed as: floor((bufferByteLength - offset) / elementSize) - Returns 0 when view is out of bounds (offset > buffer length) - updateLength() is called in isTypedArrayOutOfBounds() before bounds checks This approach minimizes code changes while maintaining compatibility with existing code that directly accesses the length field. Per ES2025 specification section 10.4.5.3
- Fix auto-length logic to only apply to resizable ArrayBuffers - Add isResizable() public method to NativeArrayBuffer - Improve JavaDoc to follow Rhino's concise documentation style - Add helper method determineViewLength() for clearer logic - Update test262.properties: 32 additional tests now passing - Array: 4 tests (find family with resize callbacks) - TypedArray: 28 tests (from, of, and prototype methods)
- Updated js_at() to call validateAndGetLength() before accessing elements - Enhanced isTypedArrayOutOfBounds() to check if fixed-length TypedArrays on resizable buffers have gone out of bounds when buffer is resized - Properly throws TypeError when accessing out-of-bounds views per ES2024
TypedArray out-of-bounds checking improvements enable 58 more tests to pass. All return-abrupt-from-this-out-of-bounds tests now passing across TypedArray prototype methods. Fixed-length TypedArray property tests on resizable buffers also passing.
|
Update - thanks for your work on this! A few things:
I really want to get this right (and use it in another project I'm working on) so I'd like to make this PR a basis for a new PR that gets this as close to the spec as we can get. Thanks! |
Implements ES2024 Resizable ArrayBuffer and ES2025 auto-length TypedArray views.
ES2024 Resizable ArrayBuffer
Constructor accepts maxByteLength option:
Resizable buffers can grow/shrink in-place:
New properties:
resizable- true if buffer has maxByteLengthmaxByteLength- maximum allowed sizeES2025 Auto-length TypedArrays
TypedArrays on resizable buffers without explicit length track buffer size dynamically:
Fixed-length views throw TypeError when buffer resizes out of bounds:
Implementation
NativeArrayBuffer:
intprimitive for maxByteLength (-1 for fixed-length)NativeTypedArrayView:
isAutoLengthflag for auto-length viewsupdateLength()recalculates length for auto-length viewsisTypedArrayOutOfBounds()validates bounds on resized buffersvalidateAndGetLength()before accessErrors:
Test262 Results
59 tests now passing (227→169 TypedArray failures)
All
return-abrupt-from-this-out-of-boundstests now pass across TypedArray methods: at, copyWithin, entries, every, fill, filter, find, findIndex, findLast, findLastIndex, forEach, includes, indexOf, join, keys, lastIndexOf, map, reduce, reduceRight, reverse, set, slice, some, sort, toLocaleString, values.Fixed-length property tests (byteLength, byteOffset, length) on resizable buffers also passing.